package com.psct.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragment;
import com.psct.Constant;
import com.psct.R;
import com.psct.ThreadPool;
import com.psct.bean.UserAccount;
import com.psct.social.UserAccountManager;
import com.psct.ui.PersonalActivity;
import com.psct.web.HttpUtil;

public class LoginFragment extends SherlockFragment implements OnClickListener {
	private String accountStr;
	private String pwdStr;
	private EditText etAccount, etPwd;
	private Button btnCancel, btnNext;
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case Constant.MESSAGE_LOGIN_OK:
				UserAccount user = (UserAccount) msg.obj;
				UserAccountManager.getInstance().setUser(user);
				UserAccountManager.getInstance().saveAccount(getActivity());
				((PersonalActivity) getActivity()).switchToPersonal();
				break;
			case Constant.MESSAGE_LOGIN_ERR:
				Toast.makeText(getActivity(), "登录失败", Toast.LENGTH_SHORT).show();
				break;
			case Constant.MESSAGE_NET_ERR:
				Toast.makeText(getActivity(), "网络异常", Toast.LENGTH_SHORT).show();
				break;
			}
		}

	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		((PersonalActivity) getActivity()).getSupportActionBar().setTitle("登录");
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_login, container, false);
		etAccount = (EditText) view.findViewById(R.id.et_account);
		etPwd = (EditText) view.findViewById(R.id.et_pwd);
		btnCancel = (Button) view.findViewById(R.id.btn_cancel);
		btnCancel.setOnClickListener(this);
		btnNext = (Button) view.findViewById(R.id.btn_next);
		btnNext.setOnClickListener(this);
		view.findViewById(R.id.btn_regist).setOnClickListener(this);
		return view;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_cancel:
			this.getActivity().finish();
			break;
		case R.id.btn_next:
			doLogin();
			break;
		case R.id.btn_regist:
			((PersonalActivity) getActivity()).switchToRegisit();
		}

	}

	/**
	 * 注册
	 */
	private void doLogin() {
		accountStr = etAccount.getText().toString();
		pwdStr = etPwd.getText().toString();
		if (!checkInput(accountStr, pwdStr))
			return;
		ThreadPool.execute(new Runnable() {

			@Override
			public void run() {
				UserAccount result = new HttpUtil(getActivity()).login(accountStr, pwdStr);

				if (result != null) {
					if (result.res==0) {// 成功
						result.pwd=pwdStr;
						Message msg = handler.obtainMessage(Constant.MESSAGE_LOGIN_OK);
						msg.obj=result;
						handler.sendMessage(msg);
					} else if (result.res==1) {// 其他
						handler.sendEmptyMessage(Constant.MESSAGE_LOGIN_ERR);
					}
				} else {// 网络异常
					handler.sendEmptyMessage(Constant.MESSAGE_NET_ERR);
				}

				handler.sendEmptyMessage(0);
			}

		});
	}

	/**
	 * 检查拼写
	 * 
	 * @param account
	 * @param pwd
	 * @param rePwd
	 * @return
	 */
	private boolean checkInput(String account, String pwd) {
		// TODO Auto-generated method stub
		if (TextUtils.isEmpty(account)) {
			Toast.makeText(getActivity(), "用户名不能为空", Toast.LENGTH_SHORT).show();
			etAccount.requestFocus();
			return false;
		}
		if (TextUtils.isEmpty(pwd)) {
			Toast.makeText(getActivity(), "密码不能为空", Toast.LENGTH_SHORT).show();
			etPwd.requestFocus();
			return false;
		}
		
		return true;
	}

}
